package com.qingxin.web.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qingxin.park.entity.BatchProducts;
import com.qingxin.park.entity.BatchProductsReport;
import com.qingxin.park.entity.BatchProductsView;
import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.Crops;
import com.qingxin.park.entity.Cropsjg;
import com.qingxin.park.entity.Cropsyz;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.park.entity.ProRecord;
import com.qingxin.park.entity.ProRecordImgs;
import com.qingxin.park.entity.ProRecordMaterial;
import com.qingxin.park.entity.WellProduct;
import com.qingxin.park.service.BatchProductsReportService;
import com.qingxin.park.service.CropsService;
import com.qingxin.park.service.CropsjgService;
import com.qingxin.park.service.CropsyzService;
import com.qingxin.park.service.ProductionLinkService;
import com.qingxin.park.service.ProductionjgLinkService;
import com.qingxin.park.service.ProductionyzLinkService;
import com.qingxin.park.vo.TraceCompany;
import com.qingxin.park.vo.TraceData;
import com.qingxin.park.vo.TraceGroup;
import com.qingxin.park.vo.TracePark;
import com.qingxin.park.vo.TraceProduct;
import com.qingxin.park.vo.TraceRecord;
import com.qingxin.park.vo.TraceRecordImg;
import com.qingxin.park.vo.TraceRecordMaterial;
import com.qingxin.park.vo.TraceReport;
import com.qingxin.park.vo.TraceWellProduct;
import com.qingxin.repertory.entity.Repertory;
import com.qingxin.repertory.service.RepertoryService;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.service.SysGroupService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.DateUtil;
import com.qingxin.sys.utils.StringUtils;

@Service
public class TraceQueryService extends BaseService<BatchProducts, String> {
	
	@Autowired
	private SysGroupService sysGroupService;
	
	@Autowired
	private CropsService cropsService;
	@Autowired
	private ProductionLinkService productionLinkService;
	
	@Autowired
	private CropsyzService cropsyzService;
	@Autowired
	private ProductionyzLinkService productionyzLinkService;
	
	@Autowired
	private CropsjgService cropsjgService;
	@Autowired
	private ProductionjgLinkService productionjgLinkService;
	
	@Autowired
	private RepertoryService repertoryService;
	@Autowired
	private BatchProductsReportService batchProductsReportService;
	
	/**
	 * 验证追溯码是否有效
	 * @param code
	 * @return
	 */
	public boolean verifyCode(String code){
		if(!code.matches("[\\d]+")){
			return false;
		}
		int sum = 0;
		for(int i=0, n=code.length()-1; i<n; i++){
			sum += Integer.parseInt(code.charAt(i)+"");
		}
		String mod = String.valueOf(sum % 10);
		return mod.equals(""+code.charAt(code.length()-1));
	}
	
	
	/**
	 * 获取追溯信息
	 * @param code
	 * @param useGroup
	 * @return
	 */
	public TraceData getTraceData(String code, boolean useGroup){
		if(StringUtils.isEmpty(code) || !verifyCode(code)){
			return new TraceData(404, "没有找到相关信息", 0);
		}
		char type = code.charAt(0);
		switch (type) {
		case '1':
			return getProductTraceData_ZZ(code, useGroup);
		case '2':
			return getProductTraceData_YZ(code, useGroup);
		case '3':
			return getProductTraceData_JG(code, useGroup);
		case '5':
			return getCompanyTraceData(code);
		}
		return new TraceData(404, "没有找到相关信息", 0);
	}
	
	
	/**
	 * 企业信息
	 * @param code
	 * @return
	 */
	public TraceData getCompanyTraceData(String code){
		String companyid = (String) getByHql("select companyid from Company where qrcode=?", code);
		if(companyid == null){
			return new TraceData(404, "没有找到相关信息", 5);
		}
		TraceData traceData = new TraceData(200, null, 5);
		TraceCompany traceCompany = getTraceCompany(companyid);
		traceData.setCompany(traceCompany);
		
//		List<TracePark> traceParks = getCompanyParks(companyid);
//		traceData.setParks(traceParks);
		
		return traceData;
	}
	
	
	
	
	/**
	 * 企业信息
	 * @param id
	 * @return
	 */
	public TraceCompany getTraceCompany(String id){
		Company company = (Company) getByHql("from Company where companyid=?", id);
		if(company == null){
			return null;
		}
		String type = "";
		switch (company.getCompanyType()) {
		case "0": type = "大户"; break;
		case "1": type = "企业"; break;
		case "2": type = "合作社"; break;
		}
		String area = "";
		String groupid = company.getGroupid();
		if(StringUtils.isNotEmpty(groupid) && groupid.length()>4){
			area = sysGroupService.getFullname(groupid.substring(0, groupid.length()-4), "");
		}
		String level = "";
		switch (company.getCompanyLevel()) {
		case "0": level = "省级"; break;
		case "1": level = "市级"; break;
		case "2": level = "地级"; break;
		}
		String productivity = "";
		switch (company.getProductivity()) {
		case "0": productivity = "0-50"; break;
		case "1": productivity = "50-200"; break;
		case "2": productivity = "200-500"; break;
		case "3": productivity = "500以上"; break;
		}
		String farmLevel = "";
		switch (company.getFarmLevel()) {
		case "0": farmLevel = "国家级"; break;
		case "1": farmLevel = "省级"; break;
		case "2": farmLevel = "市级"; break;
		case "3": farmLevel = "县级"; break;
		}
		String ccc = "";
		switch (company.getCcc()) {
		case "0": ccc = "无公害农产品"; break;
		case "1": ccc = "绿色食品"; break;
		case "2": ccc = "有机食品"; break;
		}
		
		String imgurl = UploadFile.getFileUrl(UploadFile.DIR_COMPANY_IMG, company.getImgurl());
		
		TraceCompany traceCompany = new TraceCompany(company.getCompanyid(),
				company.getCompanyName(), type, area, company.getBusinessNo(),
				company.getCorporatePerson(), company.getContactPerson(), company.getPhone(),
				company.getAddress(), level, company.getLng(), company.getLat(),
				company.getPostcodes(), productivity, company.getMainbody(),
				farmLevel, ccc, imgurl, company.getCompanyNo(), company.getRemark());
		return traceCompany;
	}
	
	
	/**
	 * 获取
	 * @param companyid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TracePark> getCompanyParks(String companyid){
		List<String> parkids = listByHql("select parkid from FarmPark where companyid=?", companyid);
		List<TracePark> traceParks = null;
		if(parkids != null && parkids.size()>0){
			traceParks = new ArrayList<>();
			for(String parkid : parkids){
				TracePark tracePark = getTracePark(parkid);
				if(tracePark != null){
					traceParks.add(tracePark);
				}
			}
		}
		return traceParks;
	}
	
	
	
	/**
	 * 园区信息
	 * @param id
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public TracePark getTracePark(String id){
		FarmPark farmPark = (FarmPark) getByHql("from FarmPark where parkid=?", id);
		if(farmPark == null){
			return null;
		}
		TracePark tracePark = new TracePark(farmPark.getParkid(), farmPark.getParkName(),
				farmPark.getParkAddress(), farmPark.getContactPerson(), farmPark.getPhone(),
				farmPark.getMobile(), farmPark.getEmail(), farmPark.getWechat(),
				farmPark.getParkLevel(), farmPark.getLat(), farmPark.getLng());
		tracePark.setImg(UploadFile.getFileUrl(UploadFile.DIR_FARM_IMG, farmPark.getLogoUrl()));
		tracePark.setRemark(farmPark.getRemark());
		
		List<WellProduct> wellProducts = listByHql("from WellProduct where parkid=?", id);
		List<TraceWellProduct> traceWellProducts = null;
		if(wellProducts != null && wellProducts.size()>0){
			traceWellProducts = new ArrayList<>();
			for(WellProduct wellProduct : wellProducts){
				String type = "";
				switch (wellProduct.getType()) {
				case "0": type="名优特色产品"; break;
				case "1": type="有机农产品"; break;
				case "2": type="绿色食品"; break;
				case "3": type="无公害农产品"; break;
				}
				String img = UploadFile.getFileUrl(UploadFile.DIR_WELLPRODUCT_IMG, wellProduct.getProductImg());
				String identification = UploadFile.getFileUrl(UploadFile.DIR_WELLPRODUCT_IMG, wellProduct.getIdentification());
				
				TraceWellProduct traceWellProduct = new TraceWellProduct(wellProduct.getProductid(),
						wellProduct.getProductName(), type, img, identification, DateUtil.fmtDate(wellProduct.getIdentificationTime()),
						wellProduct.getIntroduce());
				
				traceWellProducts.add(traceWellProduct);
			}
			
			tracePark.setWellProducts(traceWellProducts);
		}
		
		
		return tracePark;
	}
	
	
	/**
	 * 获取种植追溯数据
	 * @param code
	 * @param useGroup
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private TraceData getProductTraceData_ZZ(String code, boolean useGroup){
		BatchProductsView products = (BatchProductsView) getByHql("from BatchProductsView where pbn=? order by createTime desc", code);
		if(products == null){
			return new TraceData(404, "没有找到相关信息", 1);
		}
		TraceData traceData = new TraceData(200, null, 1);
		
		//企业
		TraceCompany traceCompany = getTraceCompany(products.getCompanyid());
		traceData.setCompany(traceCompany);
		
		//园区
		TracePark tracePark = getTracePark(products.getParkid());
		traceData.setPark(tracePark);
		
		//产品
		TraceProduct traceProduct = new TraceProduct();
		traceProduct.setId(products.getPdtid());
		traceProduct.setName(products.getPdtname());
		if(StringUtils.isNotEmpty(products.getPdtimg())){
			traceProduct.setImg(UploadFile.getFileUrl(UploadFile.DIR_PRODUCTS_IMG, products.getPdtimg()));
		}else{
			Crops crops = cropsService.get(products.getCropid());
			if(crops != null && StringUtils.isNotEmpty(crops.getCropImg())){
				traceProduct.setImg(UploadFile.getFileUrl(UploadFile.DIR_CROP_IMG, crops.getCropImg()));
			}
		}
		traceProduct.setPbn(products.getPbn());
		traceProduct.setBeginDate(DateUtil.fmtDate(products.getBeginTime()));
		traceProduct.setEndDate(DateUtil.fmtDate(products.getEndTime()));
		traceProduct.setAmount(products.getAmount());
		traceProduct.setCrop(products.getCropName());
		traceProduct.setUnits(products.getUnits());
		traceProduct.setBlockName(products.getBlockName());
		traceData.setProduct(traceProduct);
		
		//作业
		Querier querier = Querier.create("from ProRecord p" +
				" where p.isdel='0' and p.keyjob='1' and p.parkBlockPlant.plantid=:plantid" +
				" and p.workdate>=:beginTime" +
				" and p.workdate<=:endTime" +
				" order by p.workdate asc");
		querier.setParam("plantid", products.getPlantid());
		querier.setParam("beginTime", products.getBeginTime());
		querier.setParam("endTime", products.getEndTime());
		
		List<ProRecord> list = listByQuerier(querier);
		List<TraceRecord> records = new ArrayList<>();
		List<TraceGroup> groups = new ArrayList<>();
		
		TraceGroup group0 = null;
		TraceGroup group1 = null;
		TraceGroup group2 = null;
		TraceGroup group3 = null;
		TraceGroup group4 = null;
		
		if(list != null){
			for(ProRecord proRecord : list){
				TraceRecord record = new TraceRecord(proRecord.getRecordid(),
						proRecord.getLinkName(), DateUtil.fmtDate(proRecord.getWorkdate()),
						"1".equals(proRecord.getWorktime())?"上午":"下午", proRecord.getWorkerName(),
						"0".equals(proRecord.getPlantStatus())?"未成熟":"已成熟", proRecord.getRemark());
				String linkIcon = productionLinkService.getProlinkIcon(proRecord.getLinkid());
				if(StringUtils.isNotEmpty(linkIcon)){
					record.setLinkIcon( UploadFile.getFileUrl(UploadFile.DIR_PROLINK_IMG, linkIcon));
				}
				switch (proRecord.getLinkType()) {
				case 0: record.setLinkType("基本环节"); break;
				case 1: record.setLinkType("种子环节"); break;
				case 2: record.setLinkType("施肥环节"); break;
				case 3: record.setLinkType("植保环节"); break;
				case 4: record.setLinkType("采收环节"); break;
				}
				
				//图片
				Set<ProRecordImgs> imgSet = proRecord.getProRecordImgses();
				for(ProRecordImgs img : imgSet){
					record.addImg(new TraceRecordImg(img.getImgName(), UploadFile.getFileUrl(UploadFile.DIR_PRORECORD_IMG, img.getImgPath())));
				}
				
				//物资
				Set<ProRecordMaterial> proRecordMaterials = proRecord.getProRecordMaterials();
				for(ProRecordMaterial recordMaterial : proRecordMaterials){
					Repertory repertory = repertoryService.get(recordMaterial.getMaterialid());
					if(repertory != null){
						record.setMat(new TraceRecordMaterial(repertory.getRepertoryid(), repertory.getMaterialName(),
								repertory.getProducer(), UploadFile.getFileUrl(UploadFile.DIR_REP_IMG, repertory.getRepImg())));
						break;
					}
				}
				
				if(useGroup){
					switch (proRecord.getLinkType()) {
					case 0:
						if(group0 == null){
							group0 = new TraceGroup("基本环节");
						}
						group0.addRecord(record);
						break;
					case 1:
						if(group1 == null){
							group1 = new TraceGroup("种子环节");
						}
						group1.addRecord(record);
						break;
					case 2:
						if(group2 == null){
							group2 = new TraceGroup("施肥环节");
						}
						group2.addRecord(record);
						break;
					case 3:
						if(group3 == null){
							group3 = new TraceGroup("植保环节");
						}
						group3.addRecord(record);
						break;
					case  4:
						if(group4 == null){
							group4 = new TraceGroup("采收环节");
						}
						group4.addRecord(record);
						break;
					}
				}else {
					records.add(record);
				}
			}
		}
		if(useGroup){
			if(group0!= null && group0.getRecords().size()>0){
				groups.add(group0);
			}
			if(group1!= null && group1.getRecords().size()>0){
				groups.add(group1);
			}
			if(group2!= null && group2.getRecords().size()>0){
				groups.add(group2);
			}
			if(group3!= null && group3.getRecords().size()>0){
				groups.add(group3);
			}
			if(group4!= null && group4.getRecords().size()>0){
				groups.add(group4);
			}
			traceData.setGroups(groups);
		}else {
			traceData.setRecords(records);
		}
		
		List<TraceReport> reports = new ArrayList<>();
		List<BatchProductsReport> reportList = batchProductsReportService.listProductsReport(products.getPdtid());
		if(reportList !=null){
			for(BatchProductsReport report : reportList){
				reports.add(new TraceReport(report.getReportName(), UploadFile.getFileUrl(UploadFile.DIR_PRODUCTS_IMG, report.getReportUrl())));
			}
		}
		if(reports.size() > 0){
			traceData.setReports(reports);
		}
		
		return traceData;
	}
	
	
	/**
	 * 获取养殖追溯数据
	 * @param code
	 * @param useGroup
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private TraceData getProductTraceData_YZ(String code, boolean useGroup){
		BatchProductsView products = (BatchProductsView) getByHql("from BatchProductsView where pbn=? order by createTime desc", code);
		if(products == null){
			return new TraceData(404, "没有找到相关信息", 2);
		}
		TraceData traceData = new TraceData(200, null, 2);
		
		//企业
		TraceCompany traceCompany = getTraceCompany(products.getCompanyid());
		traceData.setCompany(traceCompany);
		
		//园区
		TracePark tracePark = getTracePark(products.getParkid());
		traceData.setPark(tracePark);
		
		//产品
		TraceProduct traceProduct = new TraceProduct();
		traceProduct.setId(products.getPdtid());
		traceProduct.setName(products.getPdtname());
		if(StringUtils.isNotEmpty(products.getPdtimg())){
			traceProduct.setImg(UploadFile.getFileUrl(UploadFile.DIR_PRODUCTS_IMG, products.getPdtimg()));
		}else{
			Cropsyz cropsyz = cropsyzService.get(products.getCropid());
			if(cropsyz != null && StringUtils.isNotEmpty(cropsyz.getCropImg())){
				traceProduct.setImg(UploadFile.getFileUrl(UploadFile.DIR_CROP_IMG, cropsyz.getCropImg()));
			}
		}
		traceProduct.setPbn(products.getPbn());
		traceProduct.setBeginDate(DateUtil.fmtDate(products.getBeginTime()));
		traceProduct.setEndDate(DateUtil.fmtDate(products.getEndTime()));
		traceProduct.setAmount(products.getAmount());
		traceProduct.setCrop(products.getCropName());
		traceProduct.setUnits(products.getUnits());
		traceProduct.setBlockName(products.getBlockName());
		
		//作业
		Querier querier = Querier.create("from ProRecord p" +
				" where p.keyjob='1' and p.parkBlockPlant.plantid=:plantid" +
				" and p.workdate>=:beginTime" +
				" and p.workdate<=:endTime" +
				" order by p.workdate desc");
		querier.setParam("plantid", products.getPlantid());
		querier.setParam("beginTime", products.getBeginTime());
		querier.setParam("endTime", products.getEndTime());
		
		List<ProRecord> list = listByQuerier(querier);
		List<TraceRecord> records = new ArrayList<>();
		List<TraceGroup> groups = new ArrayList<>();
		
		TraceGroup group0 = null;
		TraceGroup group1 = null;
		TraceGroup group2 = null;
		TraceGroup group3 = null;
		
		if(list != null){
			for(ProRecord proRecord : list){
				TraceRecord record = new TraceRecord(proRecord.getRecordid(),
						proRecord.getLinkName(), DateUtil.fmtDate(proRecord.getWorkdate()),
						"1".equals(proRecord.getWorktime())?"上午":"下午", proRecord.getWorkerName(),
						"0".equals(proRecord.getPlantStatus())?"未成熟":"已成熟", proRecord.getRemark());
				String linkIcon = productionyzLinkService.getProlinkIcon(proRecord.getLinkid());
				if(StringUtils.isNotEmpty(linkIcon)){
					record.setLinkIcon( UploadFile.getFileUrl(UploadFile.DIR_PROLINK_IMG, linkIcon));
				}
				switch (proRecord.getLinkType()) {
				case 0: record.setLinkType("基本环节"); break;
				case 1: record.setLinkType("喂养环节"); break;
				case 2: record.setLinkType("防疫环节"); break;
				case 3: record.setLinkType("出栏环节"); break;
				}
				
				//图片
				Set<ProRecordImgs> imgSet = proRecord.getProRecordImgses();
				for(ProRecordImgs img : imgSet){
					record.addImg(new TraceRecordImg(img.getImgName(), UploadFile.getFileUrl(UploadFile.DIR_PRORECORD_IMG, img.getImgPath())));
				}
				
				//物资
				Set<ProRecordMaterial> proRecordMaterials = proRecord.getProRecordMaterials();
				for(ProRecordMaterial recordMaterial : proRecordMaterials){
					Repertory repertory = repertoryService.get(recordMaterial.getMaterialid());
					if(repertory != null){
						record.setMat(new TraceRecordMaterial(repertory.getRepertoryid(), repertory.getMaterialName(),
								repertory.getProducer(), UploadFile.getFileUrl(UploadFile.DIR_REP_IMG, repertory.getRepImg())));
						break;
					}
				}
				
				if(useGroup){
					switch (proRecord.getLinkType()) {
					case 0:
						if(group0 == null){
							group0 = new TraceGroup("基本环节");
						}
						group0.addRecord(record);
						break;
					case 1:
						if(group1 == null){
							group1 = new TraceGroup("喂养环节");
						}
						group1.addRecord(record);
						break;
					case 2:
						if(group2 == null){
							group2 = new TraceGroup("防疫环节");
						}
						group2.addRecord(record);
						break;
					case 3:
						if(group3 == null){
							group3 = new TraceGroup("出栏环节");
						}
						group3.addRecord(record);
						break;
					}
				}else {
					records.add(record);
				}
			}
		}
		if(useGroup){
			if(group0!= null && group0.getRecords().size()>0){
				groups.add(group0);
			}
			if(group1!= null && group1.getRecords().size()>0){
				groups.add(group1);
			}
			if(group2!= null && group2.getRecords().size()>0){
				groups.add(group2);
			}
			if(group3!= null && group3.getRecords().size()>0){
				groups.add(group3);
			}
			traceData.setGroups(groups);
		}else {
			traceData.setRecords(records);
		}
		
		List<TraceReport> reports = new ArrayList<>();
		List<BatchProductsReport> reportList = batchProductsReportService.listProductsReport(products.getPdtid());
		if(reportList !=null){
			for(BatchProductsReport report : reportList){
				reports.add(new TraceReport(report.getReportName(), UploadFile.getFileUrl(UploadFile.DIR_PRODUCTS_IMG, report.getReportUrl())));
			}
		}
		if(reports.size() > 0){
			traceData.setReports(reports);
		}
		
		return traceData;
	}
	
	
	/**
	 * 获取加工追溯数据
	 * @param code
	 * @param useGroup
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private TraceData getProductTraceData_JG(String code, boolean useGroup){
		BatchProductsView products = (BatchProductsView) getByHql("from BatchProductsView where pbn=? order by createTime desc", code);
		if(products == null){
			return new TraceData(404, "没有找到相关信息", 3);
		}
		TraceData traceData = new TraceData(200, null, 3);
		
		//企业
		TraceCompany traceCompany = getTraceCompany(products.getCompanyid());
		traceData.setCompany(traceCompany);
		
		//园区
		TracePark tracePark = getTracePark(products.getParkid());
		traceData.setPark(tracePark);
		
		//产品
		TraceProduct traceProduct = new TraceProduct();
		traceProduct.setId(products.getPdtid());
		traceProduct.setName(products.getPdtname());
		if(StringUtils.isNotEmpty(products.getPdtimg())){
			traceProduct.setImg(UploadFile.getFileUrl(UploadFile.DIR_PRODUCTS_IMG, products.getPdtimg()));
		}else{
			Cropsjg cropsjg = cropsjgService.get(products.getCropid());
			if(cropsjg != null && StringUtils.isNotEmpty(cropsjg.getCropImg())){
				traceProduct.setImg(UploadFile.getFileUrl(UploadFile.DIR_CROP_IMG, cropsjg.getCropImg()));
			}
		}
		traceProduct.setPbn(products.getPbn());
		traceProduct.setBeginDate(DateUtil.fmtDate(products.getBeginTime()));
		traceProduct.setEndDate(DateUtil.fmtDate(products.getEndTime()));
		traceProduct.setAmount(products.getAmount());
		traceProduct.setCrop(products.getCropName());
		traceProduct.setUnits(products.getUnits());
		traceProduct.setBlockName(products.getBlockName());
		
		//作业
		Querier querier = Querier.create("from ProRecord p" +
				" where p.keyjob='1' and p.parkBlockPlant.plantid=:plantid" +
				" and p.workdate>=:beginTime" +
				" and p.workdate<=:endTime" +
				" order by p.workdate desc");
		querier.setParam("plantid", products.getPlantid());
		querier.setParam("beginTime", products.getBeginTime());
		querier.setParam("endTime", products.getEndTime());
		
		List<ProRecord> list = listByQuerier(querier);
		List<TraceRecord> records = new ArrayList<>();
		List<TraceGroup> groups = new ArrayList<>();
		
		TraceGroup group0 = null;
		TraceGroup group1 = null;
		TraceGroup group2 = null;
		
		if(list != null){
			for(ProRecord proRecord : list){
				TraceRecord record = new TraceRecord(proRecord.getRecordid(),
						proRecord.getLinkName(), DateUtil.fmtDate(proRecord.getWorkdate()),
						"1".equals(proRecord.getWorktime())?"上午":"下午", proRecord.getWorkerName(),
						"0".equals(proRecord.getPlantStatus())?"未成熟":"已成熟", proRecord.getRemark());
				String linkIcon = productionjgLinkService.getProlinkIcon(proRecord.getLinkid());
				if(StringUtils.isNotEmpty(linkIcon)){
					record.setLinkIcon( UploadFile.getFileUrl(UploadFile.DIR_PROLINK_IMG, linkIcon));
				}
				switch (proRecord.getLinkType()) {
				case 0: record.setLinkType("基本环节"); break;
				case 1: record.setLinkType("特殊环节"); break;
				case 2: record.setLinkType("成品环节"); break;
				}
				
				//图片
				Set<ProRecordImgs> imgSet = proRecord.getProRecordImgses();
				for(ProRecordImgs img : imgSet){
					record.addImg(new TraceRecordImg(img.getImgName(), UploadFile.getFileUrl(UploadFile.DIR_PRORECORD_IMG, img.getImgPath())));
				}
				
				//物资
				Set<ProRecordMaterial> proRecordMaterials = proRecord.getProRecordMaterials();
				for(ProRecordMaterial recordMaterial : proRecordMaterials){
					Repertory repertory = repertoryService.get(recordMaterial.getMaterialid());
					if(repertory != null){
						record.setMat(new TraceRecordMaterial(repertory.getRepertoryid(), repertory.getMaterialName(),
								repertory.getProducer(), UploadFile.getFileUrl(UploadFile.DIR_REP_IMG, repertory.getRepImg())));
						break;
					}
				}
				
				if(useGroup){
					switch (proRecord.getLinkType()) {
					case 0:
						if(group0 == null){
							group0 = new TraceGroup("基本环节");
						}
						group0.addRecord(record);
						break;
					case 1:
						if(group1 == null){
							group1 = new TraceGroup("特殊环节");
						}
						group1.addRecord(record);
						break;
					case 2:
						if(group2 == null){
							group2 = new TraceGroup("成品环节");
						}
						group2.addRecord(record);
						break;
					}
				}else {
					records.add(record);
				}
			}
		}
		if(useGroup){
			if(group0!= null && group0.getRecords().size()>0){
				groups.add(group0);
			}
			if(group1!= null && group1.getRecords().size()>0){
				groups.add(group1);
			}
			if(group2!= null && group2.getRecords().size()>0){
				groups.add(group2);
			}
			traceData.setGroups(groups);
		}else {
			traceData.setRecords(records);
		}
		
		List<TraceReport> reports = new ArrayList<>();
		List<BatchProductsReport> reportList = batchProductsReportService.listProductsReport(products.getPdtid());
		if(reportList !=null){
			for(BatchProductsReport report : reportList){
				reports.add(new TraceReport(report.getReportName(), UploadFile.getFileUrl(UploadFile.DIR_PRODUCTS_IMG, report.getReportUrl())));
			}
		}
		if(reports.size() > 0){
			traceData.setReports(reports);
		}
		
		return traceData;
	}
	
	
	
	
	
}
